package com.ezops.poc.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.ezops.businessprocess.integration.security.domain.EzopsUserProfile;

@Repository(value = "userDAO")
public class UserDAO {

	private static final String GEL_ALL_USER = "SELECT USER_ID, PASSWORD, GROUP_ID, FIRST_NAME, LAST_NAME, EMAIL, ISACTIVE, PROFILE_PIC FROM EZOPSDB.EZOPS_USER;";

	private static final String GEL_USER_BY_ID = "SELECT USER_ID, PASSWORD, GROUP_ID, FIRST_NAME, LAST_NAME, EMAIL, ISACTIVE, PROFILE_PIC FROM EZOPSDB.EZOPS_USER WHERE USER_ID=?;";

	@Autowired
	private JdbcTemplate jdbcTemplate;

	public List<EzopsUserProfile> findAll() {
		return jdbcTemplate.query(GEL_ALL_USER, new UserRowMapper());
	}

	public EzopsUserProfile findUserByLoginName(String id) {
		Object[] args = new Object[1];
		args[0] = id;
		return jdbcTemplate.query(GEL_USER_BY_ID, args, new UserRowMapper())
				.get(0);
	}

	class UserRowMapper implements RowMapper<EzopsUserProfile> {

		@Override
		public EzopsUserProfile mapRow(ResultSet rs, int rowNum)
				throws SQLException {
			EzopsUserProfile userProfile = new EzopsUserProfile();
			userProfile.setLoginName(rs.getString("USER_ID"));
			userProfile.setEmailAddress(rs.getString("EMAIL"));
			userProfile.setFirstName(rs.getString("FIRST_NAME"));
			userProfile.setLastName(rs.getString("LAST_NAME"));

			return userProfile;
		}

	}

}
